// eslint.config.mjs

import globals from 'globals';
import pluginJs from '@eslint/js';
import { configs, parser } from 'typescript-eslint';
import pluginVue from 'eslint-plugin-vue';
import eslintPluginImportX from 'eslint-plugin-import-x';
// import tsParser from '@typescript-eslint/parser'
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import oxlint from 'eslint-plugin-oxlint';
import { readFile } from 'node:fs/promises';

const autoImportFile = new URL('./.eslintrc-auto-import.json', import.meta.url);
const autoImportGlobals = JSON.parse(await readFile(autoImportFile, 'utf8'));

export default [
  // 基础配置
  {
    files: ['**/*.{js,mjs,jsx,mjsx,ts,tsx,mtsx,vue}'],
    languageOptions: {
      globals: {
        ...globals.browser,
        ...globals.node,
        ...autoImportGlobals.globals,
        $t: true,
        uni: true,
        wx: true,
        UniApp: true,
        WechatMiniprogram: true,
        getCurrentPages: true,
        UniHelper: true,
        Page: true,
        App: true,
        NodeJS: true
      },
      ecmaVersion: 'latest',
      sourceType: 'module'
    }
  },

  pluginJs.configs.recommended,
  ...configs.recommended,
  ...pluginVue.configs['flat/essential'],
  // eslint-plugin-import-x 扩展插件
  eslintPluginImportX.flatConfigs.recommended,
  eslintPluginImportX.flatConfigs.typescript,
  // prettier 扩展插件
  eslintPluginPrettierRecommended,
  // oxlint 扩展插件
  ...oxlint.configs['flat/recommended'],
  // oxlint 应该作为最后一个配置
  // 仅对所有 vue 文件的自定义配置
  {
    files: ['**/*.vue'],
    languageOptions: { parserOptions: { parser: parser } }
  },

  // 自定义规则
  {
    rules: {
      '@typescript-eslint/no-unused-vars': 'warn',
      'vue/multi-word-component-names': 'off',
      '@typescript-eslint/no-redeclare': 'error',
      'no-redeclare': 'off',
      '@typescript-eslint/no-explicit-any': 'off',
      '@typescript-eslint/no-require-imports': 'off',
      '@typescript-eslint/no-unused-expressions': 'off',
      'vue/component-name-in-template-casing': ['error', 'PascalCase'], //组件命名采用大驼峰命名
      'import-x/no-unresolved': 'off'
    }
  },

  // 忽略配置
  {
    ignores: ['src/uni_modules/', 'src/static/', '.vscode', '.husky', 'dist', 'mock', 'node_modules', 'bin/']
  }
];
